import path from 'path'

import { defineConfig, loadEnv, ConfigEnv, mergeConfig } from 'vite'
import vueJsx from '@vue3-oop/plugin-vue-jsx'
import federation from 'vite-federation';
import { reg, init } from 'vite-plugin-nacos';
import { visualizer } from 'rollup-plugin-visualizer';

export default defineConfig(async (config: ConfigEnv) => {
    const env = loadEnv(config.mode, process.cwd());

    return mergeConfig({
        plugins: [
            reg(),
            federation({
                name: env.VITE_SERVER_NAME,
                remotes: {
                    product: `${env.VITE_PREFIX}${env.VITE_NACOS_ADDRESS}/${env.VITE_MODEL_PRODUCT_NAME}/assets/remoteEntry.js`,
                    shared: `${env.VITE_PREFIX}${env.VITE_NACOS_ADDRESS}/${env.VITE_MODEL_SHARED_NAME}/assets/remoteEntry.js`
                },
                shared: {
                    vue: { requiredVersion: '^3.5.18' },
                    'vue-router': { requiredVersion: '^4.0.6' },
                    pinia: { requiredVersion: '^3.0.3' }
                }
            }),
            vueJsx(),
            visualizer({
                title: "主应用依赖分析",
                filename: 'dist/stats.html', // 分析文件输出路径
                open: false, // 构建完成后自动打开
                gzipSize: true, // 显示gzip后的大小
                brotliSize: true, // 显示brotli后的大小
            })
        ],
        build: {
            target: 'esnext',
            minify: false,
            cssCodeSplit: true,
            rollupOptions: {
                output: {
                    minifyInternalExports: false
                }
            }
        },
        resolve: {
            alias: {
                '@': path.resolve(__dirname, './src/app/'),
                '@shared': path.resolve(__dirname, './src/shared/'),
                '@features': path.resolve(__dirname, './src/features/'),
                '@domains': path.resolve(__dirname, './src/domains/')
            },
        },
    }, await init(env))
})
